package com.android.home.adapter.holder;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.common.base.rv.BaseRvAdapter;
import com.android.common.base.rv.BaseRvHolder;
import com.android.common.mvp.model.Desc;
import com.android.common.utils.InputUtils;
import com.android.home.R;
import com.luck.picture.lib.entity.LocalMedia;

public abstract class DescInputHolder extends BaseRvHolder<Desc> {

    private static final int MAX_INPUT_LENGTH = 100;

    private final Context mContext;

    private final EditText mEtDesc;

    private final TextView mTvCount;

    private final TextView mTvTitle;

    private final RecyclerView mRecyclerView;

    private final BaseRvAdapter<LocalMedia> mAdapter;


    public DescInputHolder(View itemView) {
        super(itemView);
        mContext = itemView.getContext();
        mEtDesc = itemView.findViewById(R.id.et_desc);
        mTvCount = itemView.findViewById(R.id.tv_count);
        mTvTitle = itemView.findViewById(R.id.tv_title);
        mRecyclerView = itemView.findViewById(R.id.rv_media);
        GridLayoutManager manager = new GridLayoutManager(mContext,
                3, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = initAdapter(this);
    }

    public void setTitleDesc(int selectedMultiMediaCount, int position){
        String titleDesc = String.format(mContext.getString(R.string.upload_picture_video), selectedMultiMediaCount);
        mTvTitle.setText(titleDesc);
        mEtDesc.setFilters(InputUtils.getCommonFilter(MAX_INPUT_LENGTH));
        mEtDesc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mTvCount.setText(mContext.getString(R.string.text_count, editable.length(), MAX_INPUT_LENGTH));
            }
        });
    }

    public String getFeedBackDesc(){
        return mEtDesc.getText().toString();
    }

    @Override
    public void bindData(Desc bean, int position) {
        mAdapter.setDataAndNotify(null);
        mRecyclerView.setAdapter(mAdapter);
    }

    protected abstract BaseRvAdapter<LocalMedia> initAdapter(DescInputHolder holder);
}
